package com.dict.classes;

import java.sql.ResultSet;


public class Word {

	private int id;
	private String word;
	private String meaning;
	private int UserId;
	
	public Word() {
		this.id = -1;
		this.word = "";
		this.meaning = "";
		this.UserId = -1;
	}
	
	public void load(int id) throws Exception {
		WordDb db = new WordDb();
		ResultSet rs = db.loadById(id);
		while(rs.next())
		{
			this.setId(rs.getInt(1));
			this.setWord(rs.getString(2));
			this.setMeaning(rs.getString(3));
			this.setUserId(rs.getInt(4));
		}
		db.disconnect();
	}
	
	public int store() throws Exception 
	{		
		WordDb db = new WordDb();		
		int id;
		if(this.id == -1)
			id = db.addWord( this );
		else
			id = db.updateWord( this );
		db.disconnect();
		return id;
	}
	
	public boolean delete() throws Exception
	{
		WordDb db = new WordDb();		
		boolean queryOk = db.deleteWord(this.id);		
		db.disconnect();
		return queryOk;
	}
	
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}
	public String getMeaning() {
		return meaning;
	}
	public void setMeaning(String meaning) {
		this.meaning = meaning;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUserId() {
		return UserId;
	}
	public void setUserId(int UserId){
		this.UserId = UserId;
	}
}